package org.amos.core.frame.aspect;

import cn.hutool.core.lang.UUID;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import static org.amos.core.basic.constant.SystemConstant.TRACE_ID;

/**
 * @desc: log mdc 异步任务日志切面
 * @author: liubt
 * @date: 2022-08-26 09:42
 **/
@Slf4j
@Aspect
@Component
public class LogMdcAspect {
    @Pointcut("@annotation(org.springframework.scheduling.annotation.Async)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MDC.put(TRACE_ID, UUID.randomUUID().toString(true));
        // 执行方法
        Object result = point.proceed();
        MDC.remove(TRACE_ID);
        return result;
    }
}
